package com.vimi8.ebb.api.ebb;

import com.vimi8.ebb.configuration.HttpResponseEntity;
import com.vimi8.ebb.model.Error;
import com.vimi8.ebb.model.*;
import com.vimi8.ebb.service.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.UUID;


/**
 * Created by think on 2016/12/5.
 */
@RestController
@RequestMapping("/ebb")
public class TradeDetailController
{
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TradeDetailService tradeDetailService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ElectricityBusinessService electricityBusinessService;
    @Autowired
    private GeoCodingsService geoCodingsService;
    @ApiOperation(value = "添加交易详情信息(只针对服务站点)",notes = "添加交易详情信息(只针对服务站点)",response = String.class,tags = {"平台信息(后端)"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 500, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/trade/detail/",method = RequestMethod.POST)
    public HttpResponseEntity<String> addTradeDetail(@ApiParam(value = "交易信息",required = true) @RequestBody(required = true) TradingDetail tradingDetail){
        int count=0;
        try {
            tradingDetail.setId(UUID.randomUUID().toString());
            if(StringUtils.isEmpty(tradingDetail.getTradeDate())){
                long crDate=System.currentTimeMillis()+8*60*60*1000;
                tradingDetail.setTradeDate(new Date(crDate).toString());
            }
            count=  tradeDetailService.insertTradeDetail(tradingDetail);
        } catch (Exception e) {
            return new HttpResponseEntity(new Error().error("insert tradeDetail error").errorDescription(e.getMessage()),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<String>(count+"",HttpStatus.OK);
    }

}
